/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample;

import static org.junit.Assert.assertEquals;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.global.resource.RawFileDescriptor;
import pl.droidsonroids.gif.AnimationListener;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * AnimationControlTest
 */
public class AnimationControlTest {
    private static final int FRAME_INDEX_2 = 2;
    private Context mContext;
    private ComponentContainer rootLayout;
    private GifDrawable gifDrawable ;
    private GifImageView gifView;

    /**
     * setUp
     */
    @Before
    public void setUp() {
        mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        rootLayout = (ComponentContainer) LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_animation, null, false);
        gifView = (GifImageView) rootLayout.findComponentById(ResourceTable.Id_gif);

        RawFileDescriptor rawFileDescriptor = null;
        try {
            rawFileDescriptor = mContext.getResourceManager()
                .getRawFileEntry("entry/resources/rawfile/led7.gif").openRawFileDescriptor();
            gifDrawable = new GifDrawable(rawFileDescriptor); // using assetfiledescriptor
        } catch (IOException e) {
            e.printStackTrace();
        }
        gifView.setImageElement(gifDrawable);
    }

    /**
     * testAnimationControl
     */
    @Test
    public void testAnimationControl() {
        GifDrawable element = (GifDrawable) gifView.getImageElement();
        element.getAnimator().stop();
        element.seekToFrameAndGet(FRAME_INDEX_2 - 1); // Frames starts from 0
        assertEquals(element.getCurrentFrameIndex(), FRAME_INDEX_2);
    }

    /**
     * testAnimationComplete
     */
    @Test
    public void testAnimationComplete() {
        GifDrawable element = (GifDrawable) gifView.getImageElement();
        element.setLoopCount(1);
        element.addAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationCompleted(int loopNumber) {
                element.getAnimator().stop();
                element.seekToFrameAndGet(FRAME_INDEX_2 - 1); // Frames starts from 0
                assertEquals(element.getCurrentFrameIndex(), FRAME_INDEX_2);
            }
        });
        element.getAnimator().start();
    }

}